package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.AppletsLoginBody;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.common.constant.CacheConstants.getAppletsTokenKey;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@AllArgsConstructor
public class SysLoginController {

    private final SysLoginService loginService;
    private final ISysMenuService menuService;
    private final SysPermissionService permissionService;
    private final RedisTemplate<String, String> redisTemplate;
    private final RedisCache redisCache;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 小程序登录方式
     *
     * @return 返回是否登录成功
     * @author KevenPotter
     * @date 2023-04-29 23:04:18
     */
    @Anonymous
    @PostMapping("/applets/login")
    public AjaxResult loginForApplets(@RequestBody AppletsLoginBody appletsLoginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        HashMap<String, Object> loginMap = loginService.appletsLogin(appletsLoginBody);
        ajax.put(Constants.TOKEN, loginMap.get(Constants.TOKEN));
        ajax.put(Constants.WECHAT_USER_ID, loginMap.get(Constants.WECHAT_USER_ID));
        return ajax;
    }

    /**
     * 小程序退出登录
     *
     * @return 返回是否退出登录成功
     * @author KevenPotter
     * @date 2023-04-30 10:13:18
     */
    @PostMapping("/applets/logout")
    public AjaxResult logoutForApplets(AppletsLoginBody appletsLoginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appletsLoginBody.getToken();
        String loginKey = getAppletsTokenKey(token);
        redisTemplate.opsForValue().set(loginKey, token, 60 * 24 * 7, TimeUnit.MINUTES);
        redisCache.deleteObject(loginKey);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
